"""
Case Type   : 共享存储-系统内部工具-dsscmd
Case Name   : dsscmd工具扫描并打开指定路径下指定用户和属主的盘符
Create At   : 2023/4/20
Owner       : opentestcase019
Description :
    1.扫描并打开指定路径下指定用户和属主的盘符，查看帮助信息
    2.扫描并打开指定路径下指定用户和属主的盘符，不指定参数
    3.分别在主机和备机执行扫描并打开指定路径下指定用户和属主的盘符，指定所有参数为有效值
    4.扫描并打开指定路径下指定用户和属主的盘符，指定-t type参数为无效值
    5.扫描并打开指定路径下指定用户和属主的盘符，指定-p path参数为无效值
    6.扫描并打开指定路径下指定用户和属主的盘符，指定错误的-u user_name参数;
    7.扫描并打开指定路径下指定用户和属主的盘符，指定错误的-g group_name参数;
    8.在dss关闭的情况下执行扫描并打开指定路径下指定用户和属主的盘符;
    9.在集群停止的情况下执行;
    10.D DSS_HOME路径权限不足时，扫描并打开指定路径下指定用户和属主的盘符，不指定-D DSS_HOME参数;
Expect      :
    1.查看成功
    2.合理报错
    3.扫描成功
    4.合理报错
    5.合理报错
    6.找不到对应的盘符，相当于没有新的盘符需要扫描，扫描显示成功，上层不需要再调用
    7.找不到对应的盘符，相当于没有新的盘符需要扫描，扫描显示成功，上层不需要再调用
    8.扫描成功
    9.扫描成功
    10.扫描成功
History     :
"""
import os
import time
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant

Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(1 == Primary_SH.get_node_num(), '单机环境不执行')
class ShareStorageTool(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"-----{os.path.basename(__file__)} start-----")
        self.primary_node = Node('PrimaryDbUser')
        self.primary_root = Node('PrimaryRoot')
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.standby_node = Node('Standby1DbUser')
        self.common = Common()
        self.constant = Constant()
        self.user_name = 'u_dsscmd_0036'
        self.logger.info("-----获取卷组名-----")
        execute_cmd = f"source {macro.DB_ENV_PATH};dsscmd lsvg"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn("Succeed to display lsvg info.", execute_msg,
                      "获取卷组名失败")
        self.vgname_1 = execute_msg.splitlines()[1].split()[0].strip()
        self.vgname_2 = execute_msg.splitlines()[-1].split()[0].strip()
        shell_cmd = f"source {macro.DB_ENV_PATH};" \
                    f"cat {macro.DSS_INSTANCE}/cfg/dss_vg_conf.ini"
        self.logger.info(shell_cmd)
        shell_msg = self.common.get_sh_result(self.primary_node, shell_cmd)
        self.disk_path = shell_msg.splitlines()[0].split(':')[1].strip()

    def test_sharestorage_tool(self):
        step1 = "-----step1:扫描并打开指定路径下指定用户和属主的盘符，查看帮助信息;" \
                "expect:查看成功-----"
        self.logger.info(step1)
        execute_cmd = f"source {macro.DB_ENV_PATH};dsscmd scandisk -h"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn('Usage:dsscmd scandisk <-t type> <-p path> '
                      '<-u user_name> <-g group_name>\n[client command] '
                      'Scan disk to rebuild soft link\n-t/--type <type>, '
                      '<required>, file type\n-p/--path <path>, <required>, '
                      'find disk path\n-u/--user_name <user_name>, <required>,'
                      ' user name\n-g/--group_name <group_name>, <required>, '
                      'group name', execute_msg, "执行失败" + step1)

        step2 = "-----step2:扫描并打开指定路径下指定用户和属主的盘符，" \
                "不指定参数;expect:合理报错-----"
        self.logger.info(step2)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
                      f"dsscmd scandisk;"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn('args [-t|--type] needs input value.',
                      execute_msg, "执行失败" + step2)

        step3 = "-----step3:分别在主机和备机执行扫描并打开指定路径下指定用户" \
                "和属主的盘符，指定所有参数为有效值;expect:扫描成功-----"
        self.logger.info(step3)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
                      f"dsscmd scandisk -t block -p {self.disk_path} " \
                      f"-u {self.primary_node.ssh_user} -g {self.vgname_1};" \
                      f"dsscmd scandisk --type block --path {self.disk_path}" \
                      f" --user_name {self.primary_node.ssh_user} " \
                      f"--group_name {self.vgname_1}"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertEquals(2, execute_msg.count('Succeed to scan disk.'),
                          "执行失败" + step3)

        step4 = "-----step4:扫描并打开指定路径下指定用户和属主的盘符，" \
                "指定-t type参数为无效值;expect:合理报错-----"
        self.logger.info(step4)
        cmd_list = ['ssd', '1', '%&@']
        for cmd in cmd_list:
            execute_cmd = f"source {macro.DB_ENV_PATH};" \
                    f"dsscmd scandisk -t {cmd} -p {self.disk_path} " \
                    f"-u {self.primary_node.ssh_user} -g {self.vgname_1};" \
                    f"dsscmd scandisk --type {cmd} --path {self.disk_path} " \
                    f"--user_name {self.primary_node.ssh_user} " \
                    f"--group_name {self.vgname_1}"
            self.logger.info(execute_cmd)
            execute_msg = self.common.get_sh_result(self.primary_node,
                                                    execute_cmd)
            self.assertIn('Failed to check file type, only support block type.',
                          execute_msg, "执行失败" + step4)

        step5 = "-----step5:扫描并打开指定路径下指定用户和属主的盘符，" \
                "指定-p path参数为无效值;expect:合理报错-----"
        self.logger.info(step5)
        cmd = f"source {macro.DB_ENV_PATH};" \
              f"dsscmd scandisk -t block -p /dev/not_exists " \
              f"-u {self.primary_node.ssh_user} -g {self.vgname_1};" \
              f"dsscmd scandisk --type block --path {self.disk_path}%# " \
              f"--user_name {self.primary_node.ssh_user} " \
              f"--group_name {self.vgname_1}"
        self.logger.info(cmd)
        cmd_res = self.common.get_sh_result(self.primary_node, cmd)
        self.assertIn('Succeed to scan disk', cmd_res, "执行失败" + step5)
        self.assertIn(f'Failed to check name {self.disk_path}%#', cmd_res,
                      "执行失败" + step5)

        step6 = "-----step6:扫描并打开指定路径下指定用户和属主的盘符，" \
                "指定错误的-u user_name参数;expect:找不到对应的盘符，" \
                "相当于没有新的盘符需要扫描，扫描显示成功，上层不需要再调用-----"
        self.logger.info(step6)
        self.common.create_user(self.primary_root, self.user_name)
        user_list = [f'{self.primary_node.ssh_user}_not_exists',
                     self.user_name]
        for user in user_list:
            cmd = f"source {macro.DB_ENV_PATH};" \
                  f"dsscmd scandisk -t block -p {self.disk_path} " \
                  f"-u {user} -g {self.vgname_1};" \
                  f"dsscmd scandisk --type block --path {self.disk_path} " \
                  f"--user_name {user} --group_name {self.vgname_1}"
            self.logger.info(cmd)
            cmd_res = self.common.get_sh_result(self.primary_node, cmd)
            self.assertEquals(2, cmd_res.count('Succeed to scan disk.'),
                              "执行失败" + step6)

        step7 = "-----step7:扫描并打开指定路径下指定用户和属主的盘符，" \
                "指定错误的-g group_name参数;expect:找不到对应的盘符，" \
                "相当于没有新的盘符需要扫描，扫描显示成功，上层不需要再调用-----"
        self.logger.info(step7)
        vg_list = [f'{self.vgname_1}_not_exists', self.vgname_2]
        for vg in vg_list:
            cmd = f"source {macro.DB_ENV_PATH};" \
                  f"dsscmd scandisk -t block -p {self.disk_path} " \
                  f"-u {self.primary_node.ssh_user} -g {vg};" \
                  f"dsscmd scandisk --type block --path {self.disk_path} " \
                  f"--user_name {self.primary_node.ssh_user} --group_name {vg}"
            self.logger.info(cmd)
            cmd_res = self.common.get_sh_result(self.primary_node, cmd)
            self.assertEquals(2, cmd_res.count('Succeed to scan disk.'),
                              "执行失败" + step7)

        step8 = "-----step8:在dss关闭的情况下执行扫描并打开指定路径下指定用户" \
                "和属主的盘符;expect:扫描成功-----"
        self.logger.info(step8)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
                      f"dsscmd stopdss;sleep 2;" \
                      f"dsscmd scandisk -t block -p {self.disk_path} " \
                      f"-u {self.primary_node.ssh_user} -g {self.vgname_1};" \
                      f"dsscmd scandisk --type block --path {self.disk_path}" \
                      f" --user_name {self.primary_node.ssh_user} " \
                      f"--group_name {self.vgname_1}"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.standby_node, execute_cmd)
        self.assertIn('Succeed to stop server.', execute_msg,
                      "执行失败" + step8)
        self.assertEquals(2, execute_msg.count("Succeed to scan disk."),
                          "执行失败" + step8)

        step9 = "-----step9:在集群停止的情况下执行;expect:扫描成功-----"
        self.logger.info(step9)
        stop_msg = self.primary_sh.exec_cm_ctl(mode='stop')
        self.logger.info(stop_msg)
        self.assertIn(self.constant.cm_stop_success_msg,
                      stop_msg, '执行失败' + step9)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
                      f"dsscmd scandisk -t block -p {self.disk_path} " \
                      f"-u {self.primary_node.ssh_user} -g {self.vgname_1};" \
                      f"dsscmd scandisk --type block --path {self.disk_path}" \
                      f" --user_name {self.primary_node.ssh_user} " \
                      f"--group_name {self.vgname_1}"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertEquals(2, execute_msg.count("Succeed to scan disk."),
                          "执行失败" + step9)
        start_msg = self.primary_sh.exec_cm_ctl(mode='start')
        self.logger.info(start_msg)
        self.assertIn(self.constant.cm_start_success_msg,
                      start_msg, '启动集群失败')
        time.sleep(60)
        status = self.primary_sh.exec_cm_ctl(mode='query', param='-Cv')
        self.logger.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败')

        step10 = "-----step10:DSS_HOME路径权限不足时，扫描并打开指定路径下" \
                 "指定用户和属主的盘符，不指定-D DSS_HOME参数;expect:扫描成功-----"
        self.logger.info(step10)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
                      f"chmod 000 {macro.DSS_INSTANCE};" \
                      f"dsscmd scandisk -t block -p {self.disk_path} " \
                      f"-u {self.primary_node.ssh_user} -g {self.vgname_1};" \
                      f"dsscmd scandisk --type block --path {self.disk_path}" \
                      f" --user_name {self.primary_node.ssh_user} " \
                      f"--group_name {self.vgname_1};" \
                      f"chmod 700 {macro.DSS_INSTANCE}"
        self.logger.info(execute_cmd)
        cmd_res = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertEqual(cmd_res.count("Succeed to scan disk"),
                         2, "执行失败" + step10)

    def tearDown(self):
        self.logger.info("-----无需清理环境-----")
        status = self.primary_sh.exec_cm_ctl(mode='query', param='-Cv')
        self.logger.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败')
        self.logger.info(f"-----{os.path.basename(__file__)} end-----")
